﻿using System;
using System.Collections.Generic;
using System.Linq;
using TradeMeSDK;

namespace TradeMo
{
    public class GeneralSearchCriteriaViewModel : BaseViewModel
    {
        private SearchGeneralCriteria _criteria;
        public SearchGeneralCriteria Criteria
        {
            get 
            { 
                if (_criteria == null)
                {
                    _criteria = new SearchGeneralCriteria();
                    UpdateFields();
                }
                return _criteria;
            }
            set 
            { 
                _criteria = value; 
                UpdateFields();
            }
        }

        private Locality _selectedRegion;
        public Locality SelectedRegion
        {
            get { return _selectedRegion; }
            set 
            { 
                _selectedRegion = value;
                NotifyPropertyChanged("RegionName");
                Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
            }
        }

        public string RegionName
        {
            get
            {
                return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
            }
        }

        public List<SelectorItem<Locality>> Regions
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Locality>>();
                list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
                list.AddRange(
                    App.TradeMe.Catalog.Areas.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
                return list.ToList();
            }
        }


        ////////////////////////////////////////////////////////////////////////////

        private Category _selectedCategory;
        public Category SelectedCategory
        {
            get { return _selectedCategory; }
            set
            {
                _selectedCategory = value;
                NotifyPropertyChanged("CategoryName");
                SelectedSubCategory = null;
                Criteria.Category = _selectedCategory != null ? _selectedCategory.Number : null;
            }
        }

        public string CategoryName
        {
            get
            {
                return SelectedCategory == null ? "All Categories" : SelectedCategory.Name;
            }
        }

        public List<SelectorItem<Category>> Categories
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Category>>();
                list.Add(new SelectorItem<Category> { Title = "All Categories" });
                list.AddRange(
                    App.TradeMe.Catalog.RootCategory.Subcategories.Select(
                        category => new SelectorItem<Category> { Title = category.Name, Item = category }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private Category _selectedSubCategory;
        public Category SelectedSubCategory
        {
            get { return _selectedSubCategory; }
            set
            {
                _selectedSubCategory = value;
                NotifyPropertyChanged("SubCategoryName");
                NotifyPropertyChanged("SubCategoryVisible");
                SelectedSubSubCategory = null;
                Criteria.Category = _selectedSubCategory != null ? _selectedSubCategory.Number : null;
            }
        }


        public bool SubCategoryVisible
        {
            get
            {
                if (SelectedCategory == null) return false;
                if (SelectedCategory.Subcategories == null) return false;
                return SelectedCategory.Subcategories.Count > 0;
            }
        }

        public string SubCategoryName
        {
            get
            {
                if (SelectedSubCategory == null) return "All";
                return SelectedSubCategory.Name;
            }
        }

        public List<SelectorItem<Category>> SubCategories
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!SubCategoryVisible) return null;
                var list = new List<SelectorItem<Category>>();
                list.Add(new SelectorItem<Category> { Title = "All" });
                list.AddRange(
                    SelectedCategory.Subcategories.Select(
                        category => new SelectorItem<Category> { Title = category.Name, Item = category }));
                return list.ToList();
            }
        }

        //////////////////////////

        private Category _selectedSubSubCategory;
        public Category SelectedSubSubCategory
        {
            get { return _selectedSubSubCategory; }
            set
            {
                _selectedSubSubCategory = value;
                NotifyPropertyChanged("SubSubCategoryName");
                NotifyPropertyChanged("SubSubCategoryVisible");
                Criteria.Category = _selectedSubSubCategory != null ? _selectedSubSubCategory.Number : null;
            }
        }

        public bool SubSubCategoryVisible
        {
            get
            {
                if (SelectedSubCategory == null) return false;
                if (SelectedSubCategory.Subcategories == null) return false;
                return SelectedSubCategory.Subcategories.Count > 0;
            }
        }

        public string SubSubCategoryName
        {
            get
            {
                if (SelectedSubSubCategory == null) return "All";
                return SelectedSubSubCategory.Name;
            }
        }

        public List<SelectorItem<Category>> SubSubCategories
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!SubSubCategoryVisible) return null;
                var list = new List<SelectorItem<Category>>();
                list.Add(new SelectorItem<Category> { Title = "All" });
                list.AddRange(
                    SelectedSubCategory.Subcategories.Select(
                        category => new SelectorItem<Category> { Title = category.Name, Item = category }));
                return list.ToList();
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedSortBy;
        public SelectorItem<string> SelectedSortBy
        {
            get
            {
                return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string>
                                                                 {Title = "Default"});
            }
            set 
            { 
                _selectedSortBy = value; 
                NotifyPropertyChanged("SortByName");
                Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
            }
        }
        public string SortByName
        {
            get { return SelectedSortBy.Title; }
            set { SelectedSortBy.Title = value; }   
        }

        public string SortByItem
        {
            get { return Criteria.SortOrder; }
            set { Criteria.SortOrder = value; }
        }

        public List<SelectorItem<string>> SortByItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Title", Item = "TitleAsc"},
                        new SelectorItem<string> {Title = "Closing Soon", Item = "ExpiryAsc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Most Bids", Item = "BidsMost"},
                        new SelectorItem<string> {Title = "Lowest Buy Now", Item = "BuyNowAsc"},
                        new SelectorItem<string> {Title = "Highest Buy Now", Item = "BuyNowDes"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////


        private bool _onlyBuyNow;
        public bool OnlyBuyNow
        {
            get { return _onlyBuyNow; }
            set
            {
                _onlyBuyNow = value; 
                NotifyPropertyChanged("OnlyBuyNow");
                Criteria.OnlyBuyNow = value;
            }
        }

        private bool _onlyPayNow;
        public bool OnlyPayNow
        {
            get { return _onlyPayNow; }
            set
            {
                _onlyPayNow = value; 
                NotifyPropertyChanged("OnlyPayNow");
                Criteria.OnlyPayNow = value;
            }
        }

        private bool _newCondition;
        public bool NewCondition
        {
            get { return _newCondition; }
            set
            {
                if (!value && !_newCondition && !_usedCondition && !_allCondition) return;    // deserialising

                _newCondition = value;
                NotifyPropertyChanged("NewCondition");
                if (value)
                    Criteria.Condition = "New";
            }
        }

        private bool _usedCondition;
        public bool UsedCondition
        {
            get { return _usedCondition; }
            set
            {
                if (!value && !_newCondition && !_usedCondition && !_allCondition) return;    // deserialising

                _usedCondition = value;
                NotifyPropertyChanged("UsedCondition");
                if (value)
                    Criteria.Condition = "Used";
            }
        }

        private bool _allCondition;
        public bool AllCondition
        {
            get { return _allCondition; }
            set
            {
                if (!value && !_newCondition && !_usedCondition && !_allCondition) return;    // deserialising

                _allCondition = value;
                NotifyPropertyChanged("AllCondition");
                if (value)
                    Criteria.Condition = "";
            }
        }

        public void UpdateFields()
        {
            if (string.IsNullOrEmpty(Criteria.Condition))
                _allCondition = true;
            else if (Criteria.Condition == "New")
                _newCondition = true;
            else if (Criteria.Condition == "Used")
                _usedCondition = true;


            if (Criteria.Region != 0)
            {
                SelectedRegion = App.TradeMe.Catalog.Areas.FirstOrDefault(l => l.LocalityId == Criteria.Region);
            }
            if (!string.IsNullOrEmpty(Criteria.Category))
            {
                var catTree = App.TradeMe.CategoryTree(Criteria.Category);
                if (catTree != null && catTree.Count > 0)
                {
                    SelectedCategory = catTree[0];
                    if (catTree.Count > 1)
                        SelectedSubCategory = catTree[1];
                    if (catTree.Count > 2)
                        SelectedSubSubCategory = catTree[2];
                }
            }
            if (!string.IsNullOrEmpty(Criteria.SortOrder))
            {
                SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);
            }
        }

        public void Reset()
        {
            SelectedRegion = null;
            SelectedSortBy = null;
            SelectedCategory = null;
            OnlyBuyNow = false;
            OnlyPayNow = false;
            AllCondition = true;
            UpdateFields();
        }
    }
}
